/**
 * @(#) TotalIncomProxy.as
 *
 * @date		2009. 04. 23.
 * @version		1.0
 * @author		Jo,Chang Hun
 * @description	 연말정산  - Proxy
 * 				특별공제 보험료 입력 관리 
 */
package com.skt.common_hr.yearadjustment.model
{
	
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
	import com.skt.common_hr.yearadjustment.model.vo.MedicalSubtractionVO;
	
	import mx.collections.ArrayCollection;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	

	public class MedicalProxy extends CommonProxy
	{
		public static const NAME:String = "MedicalProxy";
		public var msgStr:String;
		public var medicalData:ArrayCollection = new ArrayCollection();
		

		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function MedicalProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String, year:String):void
		{
			//CursorManager.setBusyCursor();
			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0085");
			
			var param:Object = new Object();
			param["I_YEAR"] = year;
			param["I_PERNR"] = pernr;
			param["I_YEAGBN"] = "07";		//01:보험비, 02:교육비, 03:시뇽카드 비용, 04: 현금 비용, 05:지로 비용, 06:기부금, 07:의료비
			param["I_PROC_TYPE"] = "D";		//D:조회, S:저장
			loadData("Z_HR_0085_NEW", param, result, fault);
		}
		
		public function save(parnr:String, year:String, donationArr:Array):void{
			//CursorManager.setBusyCursor();
			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0085");
			/* for(var i:int  = 0 ; i < donationArr.length; i++){
				trace("YEAGBN : " + donationArr[i].YEAGBN);
				trace("NATCT : " + donationArr[i].NATCT);
				trace("NATAM : " + donationArr[i].NATAM);
				trace("REGNO : " + donationArr[i].REGNO);
				trace("COMCD : " + donationArr[i].COMCD);
			} */
			var param:Object = new Object();
			param["I_YEAR"] = year;
			param["I_PERNR"] = parnr;
			param["I_YEAGBN"] = "07";		//01:보험비, 02:교육비, 03:시뇽카드 비용, 04: 현금 비용, 05:지로 비용, 06:기부금, 07:의료비
			param["I_PROC_TYPE"] = "S";		//D:조회, S:저장
			var table:Object = new Object();
			table["ITAB"] = donationArr;
			saveData("Z_HR_0085_NEW", param, table, saveResult, saveFault);
		}
		

		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {  
				medicalData = parseMedical(event.result.ITAB);
				facade.sendNotification(YearAdjustmentFacade.LOAD_MEDICAL_LIST_COMPLETE, this);
			}
			else {
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(YearAdjustmentFacade.LOAD_MEDICAL_LIST_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace("Fault message in medical: " + event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(YearAdjustmentFacade.LOAD_MEDICAL_LIST_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
		//저장 완료
		public function saveResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				trace( "suss to save medical proxy");
				facade.sendNotification(YearAdjustmentFacade.SAVE_MEDICAL_LIST_COMPLETE, this);
			}
			else {
				trace( "failed to save medical: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(YearAdjustmentFacade.SAVE_MEDICAL_LIST_FAILED, this);
			}
		}
		
		//저장실패
		private function saveFault(event:FaultEvent):void
		{
			trace("Fault message in medical: " + event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(YearAdjustmentFacade.SAVE_MEDICAL_LIST_FAILED, this);
		}
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 연말정산  MainList Value Object
		 */
		/**
		 * 개인정보
		 * 
		 * @param
		 * @return 특별공제 보험료 Value Object
		 */
		private function parseMedical(arr:ArrayCollection):ArrayCollection
		{
			var returnArr:ArrayCollection = new ArrayCollection();
			for each(var rec:Object in arr){
				var vo:MedicalSubtractionVO = new MedicalSubtractionVO();
				vo.SEQNO	= rec["SEQNO"];
				vo.REGNO	= rec["REGNO"];
				vo.ENAME	= rec["ENAME"];
				vo.YEAGBN	= rec["YEAGBN"];
				vo.YEASUB	= rec["YEASUB"];
				vo.YEASTX	= rec["YEASTX"];
				vo.COMCD	= rec["COMCD"];
				vo.COMNM	= rec["COMNM"];
				vo.NATCT	= rec["NATCT"];
				vo.NATAM	= rec["NATAM"];
				vo.OTHCT	= rec["OTHCT"];
				vo.OTHAM	= rec["OTHAM"];
				vo.RENUM	= rec["RENUM"];
				vo.BIGO		= rec["BIGO"];
				vo.PDCNN	= rec["PDCNN"];
				vo.PDCTX	= rec["PDCTX"];
				returnArr.addItem(vo);
			}
			
			return returnArr;
		}
		
	}
}